const EXCELJS = require('exceljs');
const fs = require('fs')
const WORKBOOK = new EXCELJS.Workbook();


const PatentType = {
    '发明': 1,
    '实用新型': 2,
    '外观设计': 3
}

// 技术领域
const Technology = {
    'A--人类生活必需': 1,
    'B--作业；运输': 2,
    'C--化学；冶金': 3,
    'D--纺织；造纸': 4,
    'E--固定建筑物': 5,
    'F--机械工程；照明；加热；武器；爆破': 6,
    'G--物理': 7,
    'H--电学': 8,
}

async function readExcel(filePath) {
    await WORKBOOK.xlsx.readFile(filePath);
    const SHEET = WORKBOOK.worksheets[0];
    let arr = []
    SHEET.eachRow((row, rowNumber) => {
        let obj = {
            "id": 'ZL'+new Date().getTime()+rowNumber,
            "title": row.getCell(1).text,
            "abs": row.getCell(9).text,
            "no": row.getCell(3).text,
            "time": '2023-05-01',
            "price": Number(row.getCell(6).text) || 0,
            "pat": PatentType[row.getCell(2).text],
            "tec": Technology[row.getCell(4).text],
            "orig": 2,
            "r":  row.getCell(7).text,
            "p": row.getCell(8).text,
        }
        arr.push(obj)
    });
    let p = {
        patent:arr
    }
    fs.writeFile('patent.json', JSON.stringify(p), () => {
        console.log('write');
    })
}

readExcel('./patent.xlsx');